package com.hurl.waguji.biz.download;

import java.sql.Time;
import java.util.Date;

import org.apache.log4j.Logger;

import com.hurl.library.spider.SpiderException;
import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.DateUtils;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.DateService;
import com.hurl.waguji.base.SqlReader;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjHqSinaBO;
import com.hurl.waguji.persistence.JcsjHqSinaBOId;

class MrhqSinaProcessor extends BaseDownloadProcessor{

	public MrhqSinaProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
		context.setBoSaveMode(DownloadContext.BO_SAVE_MODE_INSERT);
	}
	private static final Logger log = Logger.getLogger(MrhqSinaProcessor.class);
	
	private Date lastTradingTime;
	private Date lastTradingDate;
	private Date date = new Date();
	
	
	@Override
	public void validate() throws Throwable {
		lastTradingTime = DateService.getLastTradingTimeFromWeb();
		lastTradingDate = DateUtils.getDate(this.lastTradingTime);
		
		
		/* 只能在交易日当天休市后才能下载 */
		validateNowAfterTradingTime(lastTradingTime);

		/* 不可重复下载 */
//		validateAlreadyDownloaded(lastTradingDate);
	}

	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {

		String content = StringUtils.substringBetween(html, "\"", "\"");
		if(StringUtils.isBlank(content)){
			return ;
		}
		
		String[] parts = content.split(",");
		if(parts==null || parts.length==0){
			return ;
		}
		String gpdm = StringUtils.substringBetween(url, "list=", null);
		JcsjHqSinaBO bo = new JcsjHqSinaBO();
		lastTradingDate = java.sql.Date.valueOf(parts[30]);
		bo.setId(new JcsjHqSinaBOId(lastTradingDate,gpdm.toUpperCase()));
		bo.setGpMc(parts[0]);
		bo.setJrkpj(Double.parseDouble(parts[1]));
		bo.setZrspj(Double.parseDouble(parts[2]));
		bo.setDqjg(Double.parseDouble(parts[3]));
		bo.setJrzgj(Double.parseDouble(parts[4]));
		bo.setJrzdj(Double.parseDouble(parts[5]));
		bo.setBuyj(Double.parseDouble(parts[6]));
		bo.setSellj(Double.parseDouble(parts[7]));
		bo.setCjl(Long.valueOf(parts[8]));
		bo.setCjje(Double.parseDouble(parts[9]));
		bo.setBuy1sl(Long.valueOf(parts[10]));
		bo.setBuy1je(Double.parseDouble(parts[11]));
		bo.setBuy2sl(Long.valueOf(parts[12]));
		bo.setBuy2je(Double.parseDouble(parts[13]));
		bo.setBuy3sl(Long.valueOf(parts[14]));
		bo.setBuy3je(Double.parseDouble(parts[15]));
		bo.setBuy4sl(Long.valueOf(parts[16]));
		bo.setBuy4je(Double.parseDouble(parts[17]));
		bo.setBuy5sl(Long.valueOf(parts[18]));
		bo.setBuy5je(Double.parseDouble(parts[19]));
		bo.setSell1sl(Long.valueOf(parts[20]));
		bo.setSell1je(Double.parseDouble(parts[21]));
		bo.setSell2sl(Long.valueOf(parts[22]));
		bo.setSell2je(Double.parseDouble(parts[23]));
		bo.setSell3sl(Long.valueOf(parts[24]));
		bo.setSell3je(Double.parseDouble(parts[25]));
		bo.setSell4sl(Long.valueOf(parts[26]));
		bo.setSell4je(Double.parseDouble(parts[27]));
		bo.setSell5sl(Long.valueOf(parts[28]));
		bo.setSell5je(Double.parseDouble(parts[29]));
		bo.setSj(Time.valueOf(parts[31]));
		bo.setLastupdate(date);
		collector.addBO(bo);
	}

	@Override
	public void downloadFailure(String url, Throwable e) throws SpiderException {
		throw new SpiderException("下载出错",e);
	}

	@Override
	public boolean beforeDownload(String url) throws SpiderException {
		/* 该股票的交易信息如果已经下载了，就不用下载了 */
		if(context.getDao().queryBySQL(SqlReader.getSql("SQL12041403",new String[]{context.getEnvAsString("gpdm"),DateUtils.formatDate(getRq(), "yyyy-MM-dd")})).setReadOnly(true).list().size()>0){
			log.debug(context.getEnvAsString("gpdm")+"已经下载过，不用下载！");
			return false;
		}
		
		return true;
	}

	@Override
	public void shutdown(boolean success, Throwable t) {
	}

	@Override
	public Date getRq() {
		return lastTradingDate;
	}

	@Override
	public UrlPattern getUrlPattern() {
		return new StockIteratorUrlPattern(context) {
			@Override
			public String bo2url(DmGpBO bo) {
				return "http://hq.sinajs.cn/list="+bo.getGpDm().toLowerCase();
			}
		};
	}
}
